package edu.hcmus.sow.web;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import edu.hcmus.sow.dao.TaskDAO;
import edu.hcmus.sow.dao.UserActivityDAO;
import edu.hcmus.sow.domain.FileRevision;
import edu.hcmus.sow.domain.Folder;
import edu.hcmus.sow.domain.Project;
import edu.hcmus.sow.domain.Task;
import edu.hcmus.sow.domain.Topic;
import edu.hcmus.sow.domain.UserActivity;
import edu.hcmus.sow.service.DiscussionService;
import edu.hcmus.sow.service.DocumentService;
import edu.hcmus.sow.service.ProjectService;
import edu.hcmus.sow.service.SecurityService;

@Controller
public class DashboardController {
   @Autowired
   private UserActivityDAO userActivityDAO;

   @Autowired
   private DocumentService documentService;

   @Autowired
   private ProjectService projectService;

   @Autowired
   private TaskDAO taskDAO;

   @Autowired
   private DiscussionService discussionService;

   @Autowired
   private SecurityService securityService;

   @RequestMapping(value = { "", "/" })
   public String home() {
      return "forward:/dashboard";
   }

   @RequestMapping(value = "/dashboard")
   public ModelAndView displayDashboard() {
      ModelAndView mav = new ModelAndView();

      // get user activities list
      List<UserActivity> userActivities = userActivityDAO.findAll();

      List<String> map = viewDashboard(userActivities);

      // get repositories
      List<Folder> result = documentService.getRepositories();
      List<Folder> repositories = new ArrayList<Folder>();
      // has permission
      for (Folder f : result) {
         if (securityService.hasPermission(f, BasePermission.READ, BasePermission.WRITE, BasePermission.ADMINISTRATION)
               || securityService.hasRole("ROLE_ADMINISTRATOR"))
            repositories.add(f);
      }

      mav.addObject("userActivities", userActivities);
      mav.addObject("map", map);
      mav.addObject("repositories", repositories);
      mav.setViewName("dashboard");
      return mav;
   }

   public List<String> viewDashboard(List<UserActivity> userActivities) {
      ResourceBundle rb = ResourceBundle.getBundle("bundles.dashboard-resources");

      List<String> list = new ArrayList<String>();
      for (UserActivity ua : userActivities) {
         String key = "dashboard." + ua.getTargetType() + "." + ua.getAction();
         // String _param = "<a href='repository/1'>" + ua.getUser().getUsername() + "</a>";
         String _param = getTitleByType(ua.getTargetType(), ua.getTargetId());
         String value = MessageFormat.format(rb.getString(key), _param);
         list.add(value);
      }
      return list;
   }

   public String getTitleByType(String type, Integer id) {
      String _href = "<a href='";
      if ("project".equals(type)) {
         Project p = projectService.getProjectByID(id);
         return _href + "project/" + id + "'>" + p.getName() + "</a>";
      }

      if ("document".equals(type)) {
         FileRevision fr = documentService.getFileRevisionByID(id);
         return _href + "document/" + id + "'>" + fr.getFile().getName() + "</a>";
      }

      if ("task".equals(type)) {
         Task task = taskDAO.findByID(id);
         return _href + "task/" + id + "/view'>" + task.getTitle() + "</a>";
      }

      if ("discussion".equals(type)) {
         Topic topic = discussionService.getTopicByID(id);
         return _href + "topic/" + id + "/view'>" + topic.getTitle() + "</a>";
      }

      if ("comment".equals(type)) {
         Topic topic = discussionService.getTopicByID(id);
         return _href + "topic/" + id + "/view'>" + topic.getTitle() + "</a>";
      }
      return _href + "#'></a>";
   }
}
